<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="测试状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testStatus">
              <a-input v-model="model.testStatus" placeholder="请输入测试状态" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入订单号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="工程号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gc">
              <a-input v-model="model.gc" placeholder="请输入工程号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pc">
              <a-input v-model="model.pc" placeholder="请输入批次号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testItemNo">
              <a-input v-model="model.testItemNo" placeholder="请输入产品编码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testProdName">
              <a-input v-model="model.testProdName" placeholder="请输入产品名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="工序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequenceNo">
              <a-input v-model="model.sequenceNo" placeholder="请输入工序号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testProdNo">
              <a-input v-model="model.testProdNo" placeholder="请输入测试产品编码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试软件代号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testSoftCode">
              <a-input v-model="model.testSoftCode" placeholder="请输入测试软件代号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试仪器编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testInstrumentNo">
              <a-input v-model="model.testInstrumentNo" placeholder="请输入测试仪器编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试仪器名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testInstrumentName">
              <a-input v-model="model.testInstrumentName" placeholder="请输入测试仪器名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试条件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testCondition">
              <a-input v-model="model.testCondition" placeholder="请输入测试条件" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testContent">
              <a-input v-model="model.testContent" placeholder="请输入测试内容" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testExplain">
              <a-input v-model="model.testExplain" placeholder="请输入测试说明" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="当前测试产品数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testQty">
              <a-input v-model="model.testQty" placeholder="请输入当前测试产品数量" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="当前cpu的计数频率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="curCpuFrequency">
              <a-input v-model="model.curCpuFrequency" placeholder="请输入当前cpu的计数频率" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testUser">
              <a-input v-model="model.testUser" placeholder="请输入测试人员" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="检验人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectUser">
              <a-input v-model="model.inspectUser" placeholder="请输入检验人员" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="测试时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testTime">
              <j-date placeholder="请选择测试时间" v-model="model.testTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="检验结论" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="testConclusion">
              <a-input v-model="model.testConclusion" placeholder="请输入检验结论" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="预留列1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="commentCol1">
              <a-input v-model="model.commentCol1" placeholder="请输入预留列1" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="预留列2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="commentCol2">
              <a-input v-model="model.commentCol2" placeholder="请输入预留列2" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
              <a-input v-model="model.createBy" placeholder="请输入创建人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date placeholder="请选择创建日期" v-model="model.createTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="更新人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">
              <a-input v-model="model.updateBy" placeholder="请输入更新人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="更新日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateTime">
              <j-date placeholder="请选择更新日期" v-model="model.updateTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="bus_test_value" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="testValueTable.loading"
          :columns="testValueTable.columns"
          :dataSource="testValueTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="bus_test_files" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="testFilesTable.loading"
          :columns="testFilesTable.columns"
          :dataSource="testFilesTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'TestMainForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           testStatus: [
              { required: true, message: '请输入测试状态!'},
           ],
           orderNo: [
              { required: true, message: '请输入订单号!'},
           ],
           gc: [
              { required: true, message: '请输入工程号!'},
           ],
           pc: [
              { required: true, message: '请输入批次号!'},
           ],
           testItemNo: [
              { required: true, message: '请输入产品编码!'},
           ],
           testProdName: [
              { required: true, message: '请输入产品名称!'},
           ],
           sequenceNo: [
              { required: true, message: '请输入工序号!'},
           ],
           testUser: [
              { required: true, message: '请输入测试人员!'},
           ],
        },
        refKeys: ['testValue', 'testFiles', ],
        tableKeys:['testValue', 'testFiles', ],
        activeKey: 'testValue',
        // bus_test_value
        testValueTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '测试系表主键',
              key: 'id',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '测试主表主键',
              key: 'testMainId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '检验项目',
              key: 'examineProject',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '检测项目名称',
              key: 'examineProjectDescription',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '规格标准',
              key: 'defectStandard',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '规格下限',
              key: 'lowerLimit',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '规格上限',
              key: 'upperLimit',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检测值',
              key: 'defectValue',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '测量计量单位编码',
              key: 'unitOfMeasure',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '计量方式（1-计数 2-计量 3-判定）',
              key: 'measureMethod',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检测结果（0-合格 1-不合格）',
              key: 'defectResult',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '检验结论',
              key: 'defectCon',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '要求条件',
              key: 'valueTerm',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '产品编码',
              key: 'prodNo',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '预留列1',
              key: 'commentCol1',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '预留列2',
              key: 'commentCol2',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建人',
              key: 'createBy',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建日期',
              key: 'createTime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更新人',
              key: 'updateBy',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更新日期',
              key: 'updateTime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // bus_test_files
        testFilesTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '测试文件主键',
              key: 'id',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '测试主表主键',
              key: 'testMainId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '文件或目录名称',
              key: 'fileName',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '1：目录 2：文件',
              key: 'fileType',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '文件大小',
              key: 'fileSize',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '文件的MD5校验值',
              key: 'fileMd5',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '绝对路径',
              key: 'filePath',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '项目中的路径',
              key: 'fileUrl',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'Doc Node id',
              key: 'fileBucketsId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'Parent Node id',
              key: 'filePid',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'fileCreator',
              key: 'fileCreator',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'Doc CreateTime',
              key: 'fileCreateTime',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'latestEditor',
              key: 'latestEditor',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'latestEditTime',
              key: 'latestEditTime',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '状态：可用1，不可用0',
              key: 'available',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '排序码',
              key: 'ordernum',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'spread',
              key: 'spread',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: ' 上传人id',
              key: 'creatorId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '后缀名',
              key: 'fileExt',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '文件字符串',
              key: 'fileString',
               type: JVXETypes.textarea,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建人',
              key: 'createBy',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建日期',
              key: 'createTime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更新人',
              key: 'updateBy',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更新日期',
              key: 'updateTime',
              type: JVXETypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/tdm/testMain/add",
          edit: "/tdm/testMain/edit",
          queryById: "/tdm/testMain/queryById",
          testValue: {
            list: '/tdm/testMain/queryTestValueByMainId'
          },
          testFiles: {
            list: '/tdm/testMain/queryTestFilesByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.testValueTable.dataSource=[]
        this.testFilesTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.testValue.list, params, this.testValueTable)
          this.requestSubTableData(this.url.testFiles.list, params, this.testFilesTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          testValueList: allValues.tablesValue[0].tableData,
          testFilesList: allValues.tablesValue[1].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>